﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using QMSDAL;

namespace QuaManSys.CusSatiSur
{
    public partial class CusSatQuesRegist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            tbxVisitDate.Attributes.Add("ReadOnly", "true");

            displayInfomationID();
            if (!IsPostBack)
            {

            }
        }
        #region 显示质量信息编号
        //找到数据库中的最大质量信息编号
        public string latestID(string year, int kind)
        {
            var context = new QuaManSysEntities();

            if (context.CusSatSur.Count() != 0)
            {
                string std = (kind + 1).ToString();
                var query = from b in context.CusSatSur
                            where b.infomationId.Substring(4, 4).Equals(year)
                            && b.infomationId.Substring(3, 1).Equals(std)
                            orderby b.infomationId
                            select b.infomationId;

                return query.Max() != null ? query.Max() : "";
            }
            return "";
        }

        //显示质量信息编号
        public void displayInfomationID()
        {
            string infomationID = "CS0" + (DropDownList1.SelectedIndex + 1);
            infomationID += DateTime.Now.Year;
            string seq = latestID("" + DateTime.Now.Year, DropDownList1.SelectedIndex);
            if (seq.Equals(""))
            {
                infomationID += "001";
            }
            else if (!seq.Substring(4, 4).Equals(DateTime.Now.Year.ToString()))
            {
                infomationID += "001";
            }
            else
            {
                infomationID += ("" + (Convert.ToInt32(seq.Substring(8)) + 1001)).Substring(1);
            }

            lblInfomationId.Text = infomationID;
        }
        #endregion

        #region 满意度获取
        private string getSatJHQ()
        {
            if (Jhenmanyi.Checked) return Jhenmanyi.Text;
            if (Jmanyi.Checked) return Jmanyi.Text;
            if (Jyiban.Checked) return Jyiban.Text;
            return Jbumanyi.Text;
        }

        private string getSatWZ()
        {
            if (Whenmanyi.Checked) return Whenmanyi.Text;
            if (Wmanyi.Checked) return Wmanyi.Text;
            if (Wyiban.Checked) return Wyiban.Text;
            return Wbumanyi.Text;
        }

        private string getJHJH()
        {
            if (Thenmanyi.Checked) return Thenmanyi.Text;
            if (Tmanyi.Checked) return Tmanyi.Text;
            if (Tyiban.Checked) return Tyiban.Text;
            return Tbumanyi.Text;
        }

        private string getTX()
        {
            if (TXhenmanyi.Checked) return TXhenmanyi.Text;
            if (TXmanyi.Checked) return TXmanyi.Text;
            if (TXyiban.Checked) return TXyiban.Text;
            return TXbumanyi.Text;
        }

        private string getCLSD()
        {
            if (Chenmanyi.Checked) return Chenmanyi.Text;
            if (Cmanyi.Checked) return Cmanyi.Text;
            if (Cyiban.Checked) return Cyiban.Text;
            return Cbumanyi.Text;
        }

        private string getJGMY()
        {
            if (JGhenmanyi.Checked) return JGhenmanyi.Text;
            if (JGmanyi.Checked) return JGmanyi.Text;
            if (JGyiban.Checked) return JGyiban.Text;
            return JGbumanyi.Text;
        }
        #endregion

        private void submitDataInfo()
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                string satJHQ = getSatJHQ();
                string satWZ = getSatWZ();
                string satJHJH = getJHJH();
                string satTX = getTX();
                string satCLSD = getCLSD();
                string satJGMY = getJGMY();

                CusSatSur css = new CusSatSur
                {
                    infomationId = lblInfomationId.Text,
                    customName = tbxCustomName.Text,
                    department = tbxDepartment.Text,
                    visitDate = Convert.ToDateTime(tbxVisitDate.Text),
                    visitedForm = rbtInterview.Checked ? rbtInterview.Text : rbtFax.Text,
                    expectTime = Convert.ToDateTime(tbxExpectTime.Text),
                    contactMan = tbxContactMan.Text,
                    contactNum = tbxContactNum.Text,
                    satJiaohuoqi = satJHQ,
                    satWanzheng = satWZ,
                    satJiaohuojihua = satJHJH,
                    satTongxin = satTX,
                    satChulisudu = satCLSD,
                    satJieguomanyi = satJGMY,
                    instruction = txaInstruction.InnerText,
                    toBeImproved = txaToBeImproved.InnerText,
                    ourAdvantage = txaOurAdvantage.InnerText,
                    suggestion = txaSuggestion.InnerText,
                    visitedCustom = tbxVisitedCustmer.Text,
                    visitedDate = Convert.ToDateTime(tbxVisitedDate.Text),
                    cuntomTele = tbxCustomTele.Text,
                    customerClass = DropDownList1.SelectedIndex.ToString(),
                    processStatus = "汇总"
                };

                context.CusSatSur.Add(css);
                context.SaveChanges();
                CurUser user = (CurUser)Session["CurUser"];
                Common.CusSatSurRelatedDeptOperation.AddCurSatSurRelatedDept(lblInfomationId.Text, user.department);
            }
        }

        /// <summary>
        /// 提交客户满意度调查表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void submit_Click(object sender, EventArgs e)
        {
            //从session中取出当前用户信息并显示
            CurUser user = (CurUser)Session["CurUser"];
            //判断当前用户是否具有归档权限
            bool roleAccess = Permission.JudgePermission(user, "3A", "A");
            if (roleAccess)
            {
                submitDataInfo();
                fileUpload();
                Response.Redirect("Success.aspx");
            }
            else
            {
                Response.Write("@<script type=\"text/javascript\">alert('权限不够')</script>");
            }
        }

        #region 文件上传
        protected void fileUpload()
        {
            #region
            int cnt = Convert.ToInt16(hidden.Value.ToString());
            HtmlInputFile f = this.FileUploadD1;
            UpLoadFile(f.PostedFile);

            for (int i = 2; i <= cnt; i++)
            {
                HttpPostedFile f1 = Request.Files["FileUploadD" + i];
                UpLoadFile(f1);
            }
            #endregion

        }

        /// <summary>
        /// 文件上传。
        /// </summary>
        /// <param name="InputFile"></param>
        /// <param name="Path"></param>
        public string UpLoadFile(HttpPostedFile InputFile)
        {

            //判断有控件内容进入
            string b = "";
            string xdd = "";
            try
            {
                xdd = InputFile.FileName.ToString();//获取上传文件的名字
            }
            catch (Exception e)
            {
                return xdd;
            }
            if (xdd != "")
            {
                string path = "~/FileUploadCS/" + lblInfomationId.Text + "/";

                string PRICE_FJ_FJMC = "";
                string PRICE_FJ_FJHZ = "";
                string PRICE_FJ_PATH_SC = "";
                string PRICE_FJ_PATH_XZ = "";

                PRICE_FJ_PATH_XZ = path;
                //上传全路径包括文件名G:\\新建文件夹 (4)\\新建 文本文档.txt
                PRICE_FJ_PATH_SC = InputFile.FileName;
                //文件名包括后缀 新建 文本文档.txt"
                PRICE_FJ_FJMC = PRICE_FJ_PATH_SC.Substring(PRICE_FJ_PATH_SC.LastIndexOf("\\") + 1);
                ////
                //建立上传对象
                //
                HttpPostedFile postedFile = InputFile;
                //文件名包括后缀 新建 文本文档.txt"
                string fileName = System.IO.Path.GetFileName(postedFile.FileName);
                //文件后缀
                PRICE_FJ_FJHZ = System.IO.Path.GetExtension(fileName);


                string phyPath = HttpContext.Current.Request.MapPath(PRICE_FJ_PATH_XZ);//获取文件所在根目录
                //判断路径是否存在,若不存在则创建路径
                DirectoryInfo upDir = new DirectoryInfo(phyPath);
                if (!upDir.Exists)
                {
                    upDir.Create();
                }
                //
                //保存文件
                //
                try
                {
                    postedFile.SaveAs(phyPath + PRICE_FJ_FJMC);//文件保存
                    return b = phyPath;

                }
                catch
                {
                    throw new ApplicationException("上传失败!");
                }
                //返回上传文件的信息
            }

            return b;
        }

        #endregion

    }
}